package ferp.android.log;

import ferp.core.log.Log;

/**
 * User: olegs
 * Date: 7/3/12 1:29 PM
 */
public class UncaughtExceptionHandler implements Thread.UncaughtExceptionHandler
{
  public UncaughtExceptionHandler()
  {
    // save default uncaught exception handler
    this._default = Thread.getDefaultUncaughtExceptionHandler();
    Thread.setDefaultUncaughtExceptionHandler(this);
  }

  public void uncaughtException(Thread thread, Throwable t)
  {
    Log.error(Log.TAG + "/Uncaught", t);

    // temporal solution for NPE in AdMob framework
    if (!isAdMobException(thread))
      _default.uncaughtException(thread, t);
  }

  private Thread.UncaughtExceptionHandler _default;

  private boolean isAdMobException(Thread thread)
  {
    String tn = thread.getName();

    return tn != null && tn.startsWith("AdWorker");
  }
}
